package zhouyang;


import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();
//        ExecutorService threadPool = Executors.newCachedThreadPool();
//        尽量不使用Executors创建线程池，使用ThreadPoolExecutor来创建
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
/**
 * 线程池的最大容纳数为最大线程数加阻塞队列数
 * 拒绝策略：1，默认的AbortPolicy()  直接抛出异常中止系统运行
  */
        System.out.println(Runtime.getRuntime().availableProcessors());//获取系统的线程数
        try {
            for (int i = 1; i <=20 ; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        threadPool.shutdown();
    }
}
